package com.xb.cloud.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.product.merchant.service.client.bo.ShopSkuBo;
import com.xb.cloud.user.model.EndUserCart;
import com.xb.cloud.user.model.EndUserCartExample;
import com.xb.cloud.user.model.mapper.EndUserCartMapper;
import com.xb.cloud.user.service.client.ShoppingCartService;
import com.xb.cloud.user.service.client.bo.EndUserCartBo;

@Service
public class ShoppingCartServiceImpl extends AbstractService implements ShoppingCartService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);

	@Resource
	EndUserCartMapper endUserCartMapper;
	
	@Override
	public EndUserCartBo getEndUserCart(EndUserCartBo endUserCartBo) {
		if (endUserCartBo == null) {

		}

		EndUserCartExample example = new EndUserCartExample();
		example.createCriteria().andCompanyIdEqualTo(endUserCartBo.getCompanyId())
				.andEndUserIdEqualTo(endUserCartBo.getEndUserId())
				.andCartTypeEqualTo(DbConstants.SHOPPING_CART_TYPE_ENDUSER);
		List<EndUserCart> carts = endUserCartMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(carts)) {
			return null;
		}
		EndUserCart endUserCart = carts.get(0);
		endUserCartBo = BeanUtils.newAndCopyProperties(EndUserCartBo.class, endUserCart);
		return endUserCartBo;
	}

	@Override
	public int createEndUserCart(EndUserCartBo endUserCartBo) {
		EndUserCart endUserCart = BeanUtils.newAndCopyProperties(EndUserCart.class, endUserCartBo);
		endUserCart.setId(ipKeyGenerator.generateKey());
		endUserCart.setCreateTime(timeService.getDate());
		endUserCart.setCartType(DbConstants.SHOPPING_CART_TYPE_ENDUSER);
		return endUserCartMapper.insertSelective(endUserCart);
	}

	@Override
	public int updateCartValues(EndUserCartBo endUserCartBo) {
		EndUserCart endUserCart = BeanUtils.newAndCopyProperties(EndUserCart.class, endUserCartBo);
		endUserCart.setUpdateTime(timeService.getDate());
		return endUserCartMapper.updateByPrimaryKeySelective(endUserCart);
	}

}
